package com.pipe.connector.actor;

import com.pipe.exception.SessionException;
import com.pipe.protocol.BasePackage;
import com.pipe.protocol.login.OkAndEofPacket;
import com.pipe.protocol.query.ComQueryPackage;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Kiven
 */
public class RegisteredActor {
    private static Logger LOG = LoggerFactory.getLogger(RegisteredActor.class);

    void handler(BasePackage basePackage) throws SessionException {
        OkAndEofPacket okAndEofPacket = OkAndEofPacket.shortBuild(basePackage.getPayLoadByteBuf());
        if (okAndEofPacket.isOkHead() && basePackage.getPayLoadLen() >= OkAndEofPacket.OK_PACKAGE_LENGTH) {
            LOG.info("[REGISTERING] - Registered successfully!");
            return;
        }
        if (okAndEofPacket.isEofHead() && basePackage.getPayLoadLen() < OkAndEofPacket.EOF_PACKAGE_LENGTH) {
            LOG.info("[REGISTERING] - EofPacket recieved! Failed to register as a client!");
            throw new SessionException("[LOGGING] - unknown type of package recieved! handshake failed!");
        }
        LOG.info("[REGISTERING] - Package of unknown type recieved! Failed to register as a client!");
        throw new SessionException("[LOGGING] - unknown type of package recieved! handshake failed!");
    }
}